/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jcode.netty.server;

import com.jcode.netty.util.RemotingHelper;
import com.jcode.netty.util.RemotingUtil;
import io.netty.channel.Channel;
import io.netty.channel.ChannelId;

import java.net.SocketAddress;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 客户端通道
 *
 * @Author dreajay
 * @DateTime 2016年11月30日 下午5:27:33
 */
public class Session {

    enum State {
        CREATED, CONNECTING, CONNECTED, DISCONNECTING, DISCONNECTED
    }

    private final Channel channel;

    private final String sessionId;

    private State state;

    /**
     * 最后一次空闲时间
     */
    private Date lastIdleTime;
    /**
     * 空闲次数
     */
    private AtomicInteger idleTimes = new AtomicInteger(0);

    public Session(Channel channel) {
        this(channel, null);
    }

    public Session(Channel channel, String sessionId) {
        this.channel = channel;
        this.sessionId = sessionId;
    }

    public ChannelId getChannelId() {
        return channel.id();
    }

    public Date getLastIdleTime() {
        return lastIdleTime;
    }

    public void setLastIdleTime(Date lastIdleTime) {
        this.lastIdleTime = lastIdleTime;
    }

    public Integer getIdleTimes() {
        return idleTimes.get();
    }

    public Integer incrementIdleTimes() {
        return idleTimes.incrementAndGet();
    }

    public Integer decrementIdleTimes() {
        return idleTimes.decrementAndGet();
    }

    /**
     * 重置空闲次数为0
     */
    public void resetIdleTimes() {
         idleTimes.set(0);
    }


    public Channel getChannel() {
        return channel;
    }

    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }

    public String getSessionId() {
        return sessionId;
    }

    public SocketAddress getLocalAddress() {
        return channel.localAddress();
    }

    public SocketAddress getRemoteAddress() {
        return channel.remoteAddress();
    }

    public String getRemoteAddressStr() {
        return RemotingHelper.parseChannelRemoteName(channel);
    }

    @Override
    public String toString() {
        return "ClientChannelInfo [channel=" + channel + ", clientId=" + sessionId + "]";
    }
}
